<?php
require("bases.php");

class orderrecord extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$obj_catocourse = spClass('cato_course');
			$this->fmenu = $this->controller = "orderrecord";
			$this->position_one = array("text"=>"预约管理");
			$this->cato = $obj_catocourse->findAll(array('isdel'=>0, 'level_id'=>1),null,'id,name');
			$this->course = $obj_catocourse->findAll(array('isdel'=>0, 'level_id'=>2),null,'id,name');
			$this->test = $obj_catocourse->findAll(array('isdel'=>0, 'level_id'=>3),null,'id,name');
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$this->url = spUrl($this->controller, 'datalist', array("searchkeys"=>$this->searchkeys));
		$this->position_two = array("text"=>"课程预约管理", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"课程预约管理");
		$this->searchform = 1;
	}
	
	public function getcalendar(){
		try {
            $condition = '';
			$postdata = $this->spArgs();
			$action = $postdata["action"];
			$dataType = $postdata["dataType"];
			$endtime = date('Y-m-d',strtotime($postdata["endtime"]));
			$starttime = date('Y-m-d',strtotime($postdata["starttime"]));
			$status = $postdata["status"];
			$obj_order_record = spClass("order_record");
			$obj_cato = spClass("cato_course");
			$obj_course_time = Spclass('course_time');
			$course_time = $obj_course_time ->findAll(array('status'=>1, 'isdel'=>0));
			$course_time = array_column($course_time,null,'id');
			if($action != "gettodolist_calendar")
				throw new Exception("action error");
			if($dataType != '0')
				throw new Exception("datatype error");
		
			$condition .= " sys_order_record.days >= '{$starttime}' and sys_order_record.days <= '{$endtime}'";
			if(!$calendar_rs = $obj_order_record->findAll($condition))
				throw new Exception("no result");
			foreach($calendar_rs as $key => $val){
				$cato_name ='';	
				$cato_rs = $obj_cato->findAll("find_in_set(id,'{$val["cato_id"]}')",null,'id,name');
				if(!empty($cato_rs)){
					foreach($cato_rs as $k => $v){
						$cato_name.= $v['name'].',';
					}
				}
				if($val['time'] == '1,2,3,4,5'){
					$calendar_rs[$key]["all_day"] = true;
				}
				$calendar_rs[$key]["time"] = explode(',',$val['time']);
				if($calendar_rs[$key]['close_starttime'] && $calendar_rs[$key]['close_endtime']){
					$calendar_rs[$key]["start"] = $calendar_rs[$key]['close_starttime'];
					$calendar_rs[$key]["end"] = date('Y-m-d',strtotime($calendar_rs[$key]['close_endtime']));
				}else{
					$calendar_rs[$key]["start"] =$calendar_rs[$key]['days'];
					$calendar_rs[$key]["end"] = date('Y-m-d',strtotime($calendar_rs[$key]['start'])+88640);
				}
				
				$calendar_rs[$key]["cato"] = substr($cato_name,0,-1);
				$calendar_rs[$key]["type"] = "1";
				switch($val['status']){
					case '0':
						//空闲
						$title = "修改{$calendar_rs[$key]['days']}的日程";
						$calendar_rs[$key]["className"] = "dataColor endBackgroundColor";
						break;
					case '1':
						//学生占用
						$title = "查看{$calendar_rs[$key]['days']}的日程";
						$calendar_rs[$key]["className"] = "dataColor doingBackgroundColor";
						break;
					case '2':
						//教师关闭
						$title = "修改{$calendar_rs[$key]['days']}的日程";
						$calendar_rs[$key]["className"] = "dataColor hdBackgroundColor";
						break;
				}
				$calendar_rs[$key]["url"] = "javascript:zxshow({$val['id']}, '$title', '{$calendar_rs[$key]['days']}')";
			}
			$result["issuccess"] = "true";
			$result["hint"] = $calendar_rs;
			$result["course_time"] = $course_time;
			echo json_encode($result);
			exit();
		}catch(Exception $e){
			echo json_encode(array("issuccess"=>$e->getMessage()));
			exit();
		}
	}

	public function create(){
		try{
			$postdate = $this->spArgs();
			$obj_order_record = spClass('order_record');
			$obj_week = spClass('school_week');
			$obj_time = spClass('course_time');
			if(!$this->data_date = $postdate["data_date"])
				throw new Exception("日期不能为空");
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$week_rs = $obj_week->getNowWeek($this->data_date);
					if(!$week_rs)
						throw new Exception("当前日期【".$this->data_date."】未在教学周中");
					$data = array();
					$data['school_week_id'] = $week_rs["id"];
					$data['time'] = '';
					$data['cato_id'] = '';
					$data['course_id'] = '';
					$data['test_id'] = '';
					if($postdate["time"])
						$data["time"] = implode(",", $postdate["time"]);
					if($postdate["cato"])
						$data["cato_id"] = implode(",", $postdate["cato"]);
					$data['days'] = $this->data_date;
					$data['status'] = 2;
					if($result = $obj_order_record->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					if(!$obj_order_record->create($data))
						throw new Exception("未知错误，修改教学日历失败");
					$url = spUrl($this->controller, "datalist");
					$message = array('msg'=>"【".$this->data_date.'】的教学日历添加成功！','result'=>1, 'url'=>$url);
					echo json_encode($message);
					// echo $this->fetch("orderrecord/datalist.html");
					exit();

				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
			$this->course_time_rs = $obj_time->getlist();
			/*
			$this->test_select = $obj_course
				->join("sys_cato_course as level2", "level2.id = sys_cato_course.parent_id")
				->join("sys_cato_course as level1", "level1.id = sys_cato_course.parent_id1")
				->findAll(array('sys_cato_course.isdel'=>0, 'sys_cato_course.level_id'=>3), "sys_cato_course.parent_id1 asc, sys_cato_course.parent_id asc, sys_cato_course.id asc", 'sys_cato_course.id, sys_cato_course.name, level2.name as name_level2, level1.name as name_level1');
			*/
			$this->saveurl = spUrl($this->controller, "create");
		}catch(Exception $e){
			$this->redirect(spUrl("main", "index"), $e->getMessage());
			exit();
		}
	}

	public function edit(){
		try{
			$postdate = $this->spArgs();
			$obj_order_record = spClass('order_record');
			$obj_time = spClass('course_time');
			if(!$this->data_date = $postdate["data_date"])
				throw new Exception("日期不能为空");
			if(!$id = $postdate["id"])
				throw new Exception("请选择要查看的档期");
			if(!$info_rs = $obj_order_record->getInfoById($id))
				throw new Exception("找不到该日程，可能已被删除");
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					if($info_rs["student_id"] > 0)
						throw new Exception("该日程无法修改");
					$data = array();
					$data['time'] = '';
					$data['cato_id'] = '';
					$data['course_id'] = '';
					$data['test_id'] = '';
					if($postdate["time"])
						$data["time"] = implode(",", $postdate["time"]);
					if($postdate["cato"])
						$data["cato_id"] = implode(",", $postdate["cato"]);
					if($result = $obj_order_record->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					if(!$obj_order_record->update(array("id"=>$id), $data))
						throw new Exception("未知错误，修改教学日历失败");
					$url = spUrl($this->controller, "datalist");
					$message = array('msg'=>"【".$this->data_date.'】的教学日历更新成功！','result'=>1, 'url'=>$url);
					echo json_encode($message);
					exit();

				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
			$this->course_time_rs = $obj_time->getlist();
			$this->saveurl = spUrl($this->controller, "edit");
			$this->id = $id;
			if($info_rs["student_id"] > 0){
				$info_rs = $obj_order_record->getInfoByIdForStatus1($info_rs["id"]);
				if(!$info_rs)
					throw new Exception("找不到该日程，可能已被删除");
				$this->info_rs = $info_rs;
				$this->display("orderrecord/view.html");
			}else{
				$this->info_rs = $info_rs;
				$this->display("orderrecord/create.html");
			}
		}catch(Exception $e){
			$this->redirect(spUrl("main", "index"), $e->getMessage());
			exit();
		}
	}

	/*
	 * 删除日程
	 * */
	public function delete(){
		try{
			$postdate = $this->spArgs();
			$obj_order_record = spClass('order_record');
			if(!$id = $postdate["id"])
				throw new Exception("请选择要查看的档期");
			if(!$info_rs = $obj_order_record->getInfoById($id))
				throw new Exception("找不到该日程，可能已被删除");
			if($info_rs["student_id"] > 0)
				throw new Exception("该日程为学生的预定，无法删除");
			if(!$obj_order_record->delete(array("id"=>$id)))
				throw new Exception("未知错误，删除失败");
			$url = spUrl($this->controller, "datalist");
			$message = array('msg'=>"【".$info_rs["days"].'】的教学日程删除成功！','result'=>1, 'url'=>$url);
			echo json_encode($message);
			exit();
		}catch(Exception $e){
			$message = array('msg'=>$e->getMessage() ,'result'=>-1);
			echo json_encode($message);
			exit();
		}
	}

	public function details(){
		try {
			$id = intval($this->spArgs("id"));
			if(!$id)
				throw new Exception("参数丢失");
			$obj_order_record = spClass("order_record");
			$obj_cato_course = spClass("cato_course");
			if(!$calendar_rs = $obj_order_record->find(array("sys_order_record.id"=>$id)))
				throw new Exception("找不到该日程，可能已被删除");
			$this->id =$id;
			$this->info_rs = $calendar_rs;
			$this->saveurl = spUrl("orderrecord", "create");
			echo $this->fetch("orderrecord/create.html");
		}catch(Exception $e){
			header("Content-Type: text/html; charset=UTF-8");
			echo $e->getMessage();
		}
		exit();
	}

}
?>